package com.yuuzy.view;

import com.yuuzy.dao.ClientDAO;
import com.yuuzy.model.Client;
import com.yuuzy.utils.DBUtils;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class ManageClient extends JInternalFrame {
    private JTable clientTable;

    private DBUtils dbUtils = new DBUtils();
    private ClientDAO clientDAO = new ClientDAO();
    private JTextField qclient_nameTxt;
    private JTextField qclient_idTxt;
    private JTextField qclient_passwordTxt;
    private JTextField client_idTxt;
    private JTextField client_nameTxt;
    private JTextField client_telephoneTxt;
    private JTextField client_idnumberTxt;
    private JTextField client_emailTxt;
    private JTextField client_passwordTxt;
    private JTextField client_balanceTxt;

    /**
     * Create the frame.
     */
    public ManageClient() {
        setMaximizable(true);
        setResizable(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("客户查询");
        setBounds(100, 100, 891, 956);

        JScrollPane scrollPane = new JScrollPane();

        JLabel label = new JLabel("根据客户姓名查询：");
        label.setFont(new Font("宋体", Font.PLAIN, 19));

        JLabel lblNewLabel = new JLabel("客户姓名：");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 19));

        qclient_nameTxt = new JTextField();
        qclient_nameTxt.setColumns(10);


        JLabel lblNewLabel_1 = new JLabel("根据客户id和密码查询：");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 19));

        JLabel lblNewLabel_2 = new JLabel("客户id:");
        lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 19));

        qclient_idTxt = new JTextField();
        qclient_idTxt.setColumns(10);

        JLabel label_1 = new JLabel("客户密码：");
        label_1.setFont(new Font("宋体", Font.PLAIN, 19));

        qclient_passwordTxt = new JTextField();
        qclient_passwordTxt.setColumns(10);

        JButton button = new JButton("查询");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clientQueryActionPerformed(e);
            }
        });

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "\u4FEE\u6539/\u5220\u9664\u5BA2\u6237\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(64)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addComponent(lblNewLabel)
                                                .addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                                                .addComponent(qclient_nameTxt, GroupLayout.PREFERRED_SIZE, 252, GroupLayout.PREFERRED_SIZE))
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(label)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel_2)
                                                        .addComponent(label_1))
                                                .addGap(34)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(qclient_idTxt, GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                                                        .addComponent(qclient_passwordTxt, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE))))
                                .addGap(209)
                                .addComponent(button)
                                .addGap(151))
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(25)
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(panel, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                        .addComponent(scrollPane, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 805, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(209, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(28)
                                .addComponent(label)
                                .addGap(18)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(qclient_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(26)
                                .addComponent(lblNewLabel_1)
                                .addGap(27)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(qclient_idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_2))
                                .addGap(36)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(qclient_passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_1)
                                        .addComponent(button))
                                .addGap(18)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE)
                                .addGap(34)
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 290, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(32, Short.MAX_VALUE))
        );

        JLabel lblNewLabel_3 = new JLabel("客户id:");

        JLabel lblNewLabel_4 = new JLabel("客户姓名：");

        JLabel lblNewLabel_5 = new JLabel("客户电话：");

        JLabel lblNewLabel_6 = new JLabel("客户身份证：");

        JLabel lblNewLabel_7 = new JLabel("客户邮箱：");

        JLabel lblNewLabel_8 = new JLabel("客户密码：");

        client_idTxt = new JTextField();
        client_idTxt.setEditable(false);
        client_idTxt.setColumns(10);

        client_nameTxt = new JTextField();
        client_nameTxt.setColumns(10);

        client_telephoneTxt = new JTextField();
        client_telephoneTxt.setColumns(10);

        client_idnumberTxt = new JTextField();
        client_idnumberTxt.setColumns(10);

        client_emailTxt = new JTextField();
        client_emailTxt.setColumns(10);

        client_passwordTxt = new JTextField();
        client_passwordTxt.setColumns(10);

        JButton btnNewButton = new JButton("修改");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clientUpdateActionEvent(e);
            }
        });

        JButton btnNewButton_1 = new JButton("删除");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clientDeleteActionEvent(e);
            }
        });

        JLabel label_2 = new JLabel("客户余额：");

        client_balanceTxt = new JTextField();
        client_balanceTxt.setEditable(false);
        client_balanceTxt.setColumns(10);
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(36)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel_3)
                                                        .addComponent(lblNewLabel_4)
                                                        .addComponent(lblNewLabel_5))
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                                        .addComponent(client_telephoneTxt)
                                                        .addComponent(client_nameTxt)
                                                        .addComponent(client_idTxt, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE))
                                                .addGap(70)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel_6)
                                                        .addComponent(lblNewLabel_7)
                                                        .addComponent(lblNewLabel_8))
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                                        .addComponent(client_passwordTxt)
                                                        .addComponent(client_emailTxt)
                                                        .addComponent(client_idnumberTxt, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(client_balanceTxt, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btnNewButton)
                                                .addGap(98)
                                                .addComponent(btnNewButton_1)))
                                .addContainerGap(142, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(28)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_3)
                                        .addComponent(lblNewLabel_6)
                                        .addComponent(client_idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(client_idnumberTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(29)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_4)
                                        .addComponent(lblNewLabel_7)
                                        .addComponent(client_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(client_emailTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(26)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_5)
                                        .addComponent(lblNewLabel_8)
                                        .addComponent(client_telephoneTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(client_passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(27)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton_1)
                                        .addComponent(btnNewButton)
                                        .addComponent(label_2)
                                        .addComponent(client_balanceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
        );
        panel.setLayout(gl_panel);

        clientTable = new JTable();
        clientTable.addMouseListener(new MouseAdapter() {
            @Override
            //鼠标点击事件
            public void mousePressed(MouseEvent e) {
                clientTableMousePressed(e);
            }
        });
        clientTable.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "\u5BA2\u6237\u7F16\u53F7", "\u5BA2\u6237\u59D3\u540D", "\u8054\u7CFB\u65B9\u5F0F", "\u8EAB\u4EFD\u8BC1\u53F7", "\u7535\u5B50\u90AE\u7BB1", "\u5BA2\u6237\u5BC6\u7801", "\u5BA2\u6237\u4F59\u989D"
                }
        ) {
            boolean[] columnEditables = new boolean[]{
                    false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        clientTable.getColumnModel().getColumn(0).setPreferredWidth(93);
        clientTable.getColumnModel().getColumn(1).setPreferredWidth(112);
        clientTable.getColumnModel().getColumn(2).setPreferredWidth(159);
        clientTable.getColumnModel().getColumn(3).setPreferredWidth(155);
        clientTable.getColumnModel().getColumn(4).setPreferredWidth(114);
        clientTable.getColumnModel().getColumn(5).setPreferredWidth(110);
        clientTable.getColumnModel().getColumn(6).setPreferredWidth(109);
        scrollPane.setViewportView(clientTable);
        getContentPane().setLayout(groupLayout);

        this.fillTable(new Client());

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ManageClient frame = new ManageClient();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //删除
    private void clientDeleteActionEvent(ActionEvent evt) {
        String client_id = client_idTxt.getText();
        String client_name = client_nameTxt.getText();
        String client_telephone = client_telephoneTxt.getText();
        String client_idnumber = client_idnumberTxt.getText();
        String client_email = client_emailTxt.getText();
        String client_password = client_passwordTxt.getText();
        String client_balance = client_balanceTxt.getText();
        if (StringUtils.isEmpty(client_id)) {
            JOptionPane.showMessageDialog(null, "请选择要删除的记录");
            return;
        }
        int n = JOptionPane.showConfirmDialog(null, "确定要删除该客户吗？");
        if (n == 0) {
            Client client = new Client(Integer.parseInt(client_id), client_name);
            int deletenum = clientDAO.deleteClient(client);
            if (deletenum == 1) {
                JOptionPane.showMessageDialog(null, "删除成功！");
                this.resetValue();
                this.fillTable(new Client());
            } else {
                JOptionPane.showMessageDialog(null, "删除失败！");
            }

        }
    }

    //修改
    private void clientUpdateActionEvent(ActionEvent evt) {
        String client_id = client_idTxt.getText();
        String client_name = client_nameTxt.getText();
        String client_telephone = client_telephoneTxt.getText();
        String client_idnumber = client_idnumberTxt.getText();
        String client_email = client_emailTxt.getText();
        String client_password = client_passwordTxt.getText();
        String client_balance = client_balanceTxt.getText();
        if (StringUtils.isEmpty(client_id)) {
            JOptionPane.showMessageDialog(null, "请选择要修改的记录");
            return;
        }

        if (StringUtils.isEmpty(client_name)) {
            JOptionPane.showMessageDialog(null, "用户姓名不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_telephone)) {
            JOptionPane.showMessageDialog(null, "用户电话不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_idnumber)) {
            JOptionPane.showMessageDialog(null, "用户身份证不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_email)) {
            JOptionPane.showMessageDialog(null, "用户邮箱不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_password)) {
            JOptionPane.showMessageDialog(null, "用户密码不能为空!");
            return;
        }

        Client client = new Client(Integer.parseInt(client_id), client_name, client_telephone, client_idnumber, client_email, client_password, StringUtils.changeDouble(client_balance));
        int updatenum = clientDAO.updateClient(client);
        if (updatenum > 0) {
            JOptionPane.showMessageDialog(null, "修改成功！");
            this.resetValue();
            this.fillTable(new Client());

        } else {
            JOptionPane.showMessageDialog(null, "修改失败！");
        }
    }

    //重置表单
    private void resetValue() {
        this.client_idTxt.setText("");
        this.client_nameTxt.setText("");
        this.client_telephoneTxt.setText("");
        this.client_idnumberTxt.setText("");
        this.client_emailTxt.setText("");
        this.client_passwordTxt.setText("");
    }

    //表格行点击事件处理
    private void clientTableMousePressed(MouseEvent evt) {
        int row = clientTable.getSelectedRow();
        client_idTxt.setText((Integer.toString((int) clientTable.getValueAt(row, 0))));
        client_nameTxt.setText((String) clientTable.getValueAt(row, 1));
        client_telephoneTxt.setText((String) clientTable.getValueAt(row, 2));
        client_idnumberTxt.setText((String) clientTable.getValueAt(row, 3));
        client_emailTxt.setText((String) clientTable.getValueAt(row, 4));
        client_passwordTxt.setText((String) clientTable.getValueAt(row, 5));
        client_balanceTxt.setText((Double.toString((double) clientTable.getValueAt(row, 6))));

    }

    //查询
    private void clientQueryActionPerformed(ActionEvent evt) {
        String client_name = this.qclient_nameTxt.getText();
        String client_id = this.qclient_idTxt.getText();
        String client_password = this.qclient_passwordTxt.getText();
        Client client = new Client();
        client.setClient_name(client_name);
        if (!StringUtils.isEmpty(client_id)) {
            client.setClient_id(StringUtils.changeInt(client_id));
        }
        client.setClient_password(client_password);
        this.fillTable(client);
    }

    //初始化表格
    private void fillTable(Client client) {
        DefaultTableModel dtm = (DefaultTableModel) clientTable.getModel();
        dtm.setRowCount(0);//设置为0行，清空表格
        List<Client> clientList = clientDAO.queryClient(client);
        for (Client rs : clientList) {
            Vector v = new Vector();
            v.add(rs.getClient_id());
            v.add(rs.getClient_name());
            v.add(rs.getClient_telephone());
            v.add(rs.getClient_idnumber());
            v.add(rs.getClient_email());
            v.add(rs.getClient_password());
            v.add(rs.getClient_balance());
            dtm.addRow(v);
        }
    }
}